package com.huiqiao.lesson8.mybatis.service;


import com.huiqiao.lesson8.mybatis.entity.SysUser;
import com.huiqiao.lesson8.mybatis.mapper.SysUserMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 注册用户，默认注册普通用户（01）
     * 密码转储密文存储
     */
    public int register(SysUser user) {
        SysUser oldUser = userMapper.selectUserByUserName(user.getUserName());
        if (oldUser != null) {
            log.error("注册失败，username已存在");
            return -1;
        }

        oldUser = userMapper.selectUserByPhone(user.getPhonenumber());
        if (oldUser != null) {
            log.error("注册失败，phonenumber已存在");
            return -1;
        }

        user.setUserType("01");
        int row = userMapper.insertUser(user);
        if (row > 0) {
            log.info("用户名：{} 注册成功", user.getUserName());
            log.error("注册成功");
        } else {
            log.error("注册失败");
        }
        return row;
    }


}
